// 购物按钮
import {
	getCurrentInstance,
	toRefs,
	reactive,
	onMounted,
	computed,
	watch
} from 'vue'
import {useRoute}  from 'vue-router'
import {useStore}  from 'vuex'

function cartModel(props){
	
	const $store = useStore();
	const data = reactive({
		aName:'',
		show:false,
		//加购状态
		foodState:computed(()=>{
			return $store.state.foodState;
		}),
		state:computed(()=>{
			//根据商场id查询商品信息
			return data.foodState[props.resId];
		}),
		//总数量
		count_all:computed(()=>{
			if(data.state){
				return data.state.count_all;
			}
		}),
		rst:computed(()=>{
			return $store.getters.rst
		}),
		// 商品信息
		foods:computed(()=>{
			if(data.state){
				return data.state.foods;
			}
		})
	})
	//起送价格
	const base_price = computed(()=>{
		return data.rst.piecewise_agent_fee.rules[0].price
	})
	
	//计算商品总结
	const allPrice = computed(()=>{
		//当前商家 加购的 商品信息
		var state = data.state;
		var price = 0,oPrice = 0;
		
		//存放商品信息
		var food,
			_price,
			_oPrice,
			_priceAll;
		// console.log(food);
		if(state){
			var foods = state.foods;//商品信息
			//遍历加购的商品的信息
			for(var k in foods){
				food = foods[k];
				//得到当前商品
				_priceAll = food.item.specfoods[0];
				//商品的现价
				_price = _priceAll.price;
				//计算现价的总价
				price += _price * food.count;
				
				//商品原价
				_oPrice = _priceAll.original_price;
				//计算原价总价
				oPrice += _oPrice?_oPrice * food.count : price * food.count;
			}
			price = price.toFixed(1);
			oPrice = oPrice.toFixed(1);
			return {price:price,oPrice:oPrice}
		}
		return {price:0,oPrice:0}
	})
	
	//组件挂载完成之后
	onMounted(()=>{
		// console.log(props.resId)
		//处理购物车的动画效果
		watch(()=>data.count_all,(n,o)=>{
			//判断商品数量++ 设置动画名称
			if(n>(o|0)){
				data.aName='';
				setInterval(()=>{
					data.aName="car 0.3s"
				},100)
			}
		})
		//侦听foodState
		watch([data.foodState,()=>data.state],([fv,sv],[ofv,osv])=>{
			// console.log(sv)
		},{
			/* 侦听某个对象具体的值，必须开启深度侦听，否则侦听不到值 */
			deep:true//开启深度侦听
		})
	})
	return{
		allPrice,base_price,
		...toRefs(data)
	}
	
}
export default cartModel;